package net.siufung.simple.config.security;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.security.api.properties.SecurityProperties;
import net.siufung.security.api.service.IAccessTokenService;
import net.siufung.security.api.service.ICurrentUserPasswdService;
import net.siufung.security.base.service.ICurrentUserService;
import net.siufung.security.granter.DefaultTokenGranterProvider;
import net.siufung.security.provider.AccessTokenAuthenticationProvider;
import net.siufung.security.provider.PasswdAuthenticationProvider;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.stereotype.Component;

/**
 * @author 陈建峰
 * @since 2022/5/6 4:35 下午
 */
@SuppressWarnings("deprecation")
@Slf4j
@Configuration
@AllArgsConstructor
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableConfigurationProperties({SecurityProperties.class})
public class SecurityAutoConfig extends WebSecurityConfigurerAdapter {

    private final ICurrentUserService currentUserService;
    private final IAccessTokenService accessTokenService;
    private final ICurrentUserPasswdService currentUserPasswdService;
    private final SecurityProperties securityProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .csrf().disable().cors();
        http
                .anonymous().disable();
        http
                .formLogin().disable();
        http
                .logout().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(passwdAuthenticationProvider())
                .authenticationProvider(accessTokenAuthenticationProvider())
                .eraseCredentials(false);
    }

    @Bean
    public AuthenticationProvider passwdAuthenticationProvider(){
        PasswdAuthenticationProvider authProvider = new PasswdAuthenticationProvider();
        authProvider.setCurrentUserService(currentUserService);
        authProvider.setCurrentUserPasswdService(currentUserPasswdService);
        authProvider.setSecurityProperties(securityProperties);
        return authProvider;
    }

    @Bean
    public AuthenticationProvider accessTokenAuthenticationProvider(){
        AccessTokenAuthenticationProvider authProvider = new AccessTokenAuthenticationProvider();
        authProvider.setCurrentUserService(currentUserService);
        authProvider.setAccessTokenService(accessTokenService);
        return authProvider;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return currentUserService.getUserPasswordEncoder();
    }

    @Override
    @Bean(name = "authenticationManager")
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public TokenStore tokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) {
        return new JwtTokenStore(jwtAccessTokenConverter);
    }

    @Component("tokenGranterProvider")
    @Primary
    public static class TokenGranterProviderImpl extends DefaultTokenGranterProvider {}
}
